<?php

/**
 * @author
 * @copyright 2014
 */
class Loaigiaoban extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model();
        //$this->lang->load('title');
    }
    function _remap($method, $params = array()) {
        if (method_exists($this, $method)) {
            return call_user_func_array(array($this, $method), $params);
        }
        $this->_view();
    }
    function _view() {
		$data = new stdClass();
        $this->load->helper('form');
        $data->page = 0;
        $title = "Loại giao ban"; 
		
		$controller = $this->uri->segment(1);
		$data->cotrol = $controller;
		$right = $this->login_model->permission($controller); 
		if(!isset($right['view'])){
			redirect(base_url().'home');
		}
		$data->rights = $right;
		
        $content = $this->load->view('view', $data, true);
		$this->pub->write('title',$title,true);
		$this->pub->write('content',$content,true);
        $this->pub->render();
    }
	function getList(){
		 $data = new stdClass();
		 $result = new stdClass();
		 $page=$this->input->post('page');
         $counts=$this->input->post('count');
		 if(empty($counts)) $counts = $this->pub->config['numrows'];
		 $search = json_decode($this->input->post('search'));	
		
		 $controller = $this->uri->segment(1);
		 $right = $this->login_model->permission($controller); 
		 $data->rights = $right;
		 
		 
		 $dept = $this->model->where('isdelete',0);
		 if(!empty($search->name)){
			$dept = $dept->like('names',$search->name);
		 }
		 $count    = $dept->count_all(false); 
         if($counts!='Tất cả'){
           $dept = $dept->limit($counts,$page);
		 }
         else{
            $counts = $count;
		 }
         $dept = $dept->order_by('names','ASC')->find_all(false);
		 $page_view='&nbsp;';
        
		 if($count>0){
			$page_view=$this->pub->phantrang($count,$counts,5,'loaigiaoban/',$page);
		 }
         if(empty($page_view)) $page_view='&nbsp;';
         $result->paging = $page_view;
         $result->cPage = $page;
		 $data->datas = $dept;
		 $data->count = $count;
         $result->content = $this->load->view('list',$data,true);
         $result->action = $this->pub->action;
		 echo json_encode($result);
	}
	function insert(){
		 $this->load->helper('form');
		 $data = new stdClass();
		 $id =  $this->input->post('id');	
		 $query = $this->model->where('id',$id)->find();

		 $data->datas = $query;
		 $data->id = $id;
		 echo $this->load->view('form', $data, true);	
	}
	function save(){
		$str = $this->input->post('str');
		$id = $this->input->post('id');		
		if($str !='{}'){
			$json = json_decode($str);
			$data = array();
			foreach($json as $clm=>$val){
				$data[$clm] = $val;
			}
			$result = $this->model->save($data,$id);
			echo $result;
		}
		else{
			echo 0;	
		}
	}
	function delete(){
		$str = $this->input->post('str');
		$item = $this->input->post('item');
		$this->model->delete($str,$item);
	}
	function loadSearch(){
		$key = $this->input->post('key');
		$cartype = $this->model->where('isdelete',0)
							   ->select('typename')	
							   ->like('typename',$key) 
							   ->limit(10)
							   ->find_all();
		$str = "";
		foreach($cartype as $item){
			$str.= ','.($item->typename).'';
		}
		$str = substr($str,1);
		echo $str; 
	}
}